package com.lonbon.threecameramaster.test;

import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

public class Shell {
    public enum User {ROOT, SHELL}
    private static final String TAG = Shell.class.getSimpleName();

    private static final int SHELL_UID   =   2000;
    private static final int SHELL_GID   =   2000;

    private static final int ROOT_UID    =   0;
    private static final int ROOT_GID    =   0;

    public static int run(String[] cmd, int uid, int gid, StringBuffer output) {
        int ret = -1;

        if (cmd != null && cmd.length > 0) {
            Process p = null;
            BufferedReader in = null;
            try {
                String[] tmp = new String[cmd.length + 2];
                if (new File("/system/xbin/xsu").exists()) {
                    tmp[0] = "/system/xbin/xsu";
                } else if (new File("/system/xbin/su").exists()) {
                    tmp[0] = "/system/xbin/su";
                } else {
                    return -1;
                }
                tmp[1] = String.format("%d,%d", uid, gid);
                System.arraycopy(cmd, 0, tmp, 2, cmd.length);
                //p = Runtime.getRuntime().exec(tmp);
                ProcessBuilder pb = new ProcessBuilder(tmp);
                pb.redirectErrorStream(true);
                p = pb.start();

                in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String s;
                while ((s = in.readLine()) != null) {
                    if (output != null) {
                        output.append(s).append("\n");
                    }
                }
                ret = p.waitFor();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                p.destroy();
            }
            if (output != null && output.length() > 0) {
                Log.e(TAG, output.toString());
            }
        }

        return ret;
    }

    public static int run(String[] cmd, User user, StringBuffer output) {
        if (user == User.ROOT) {
            return run(cmd, ROOT_UID, ROOT_GID, output);
        }
        return run(cmd, SHELL_UID, SHELL_GID, output);
    }

    public static int run(String[] cmd, User user) {
        return run(cmd, user, null);
    }

    public static int run(String[] cmd) {
        return run(cmd, User.SHELL);
    }

    public static int setprop(String key, String value) {
        return run(new String[] { "setprop", key, value });
    }

    public static int setprop(String key, boolean value) {
        return setprop(key, String.valueOf(value));
    }

    public static int setprop(String key, int value) {
        return setprop(key, String.valueOf(value));
    }

    public static int setprop(String key, long value) {
        return setprop(key, String.valueOf(value));
    }

    public static String getprop(String key, String defVal) {
        String val = null;
        try {
            Class<?> classType = Class.forName("android.os.SystemProperties");
            Method getMethod = classType.getDeclaredMethod("get", String.class);
            val = (String) getMethod.invoke(classType, key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return TextUtils.isEmpty(val) ? defVal : val;
    }

    public static String getprop(String key) {
        return getprop(key, null);
    }
}
